<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-2.1.0.min.js">
</script>
<script type="text/javascript">
	$(function(){
		
		$.ajax({
			url:"${pageContext.request.contextPath}/userServlet",
			type:"post",
			cache:"false",
			data:{"action":"findAllTelbook"},
			success:function(data){
				var str="点击通信录,查看联系人:";
				for(var i=0;i<data.length;i++){
					var bn=data[i].tname;
					var tid=data[i].tid;
					str+="<br />";
					str+="<input type=\"button\"value=\""+bn+"\"style=\"margin-top: 20px\"onclick=\"showContacts("+tid+")\">";
				}
				$("#div1").html(str);
			},
			error:function(){
				
			}
		});
			
});
	function showContacts(tid){
		
		$.ajax({
			url:"${pageContext.request.contextPath}/userServlet",
			type:"post",
			cache:"false",
			data:{"action":"findContacts","tid":tid},
			success:function(data){
			
				var string="";
				for(var i=0;i<data.length;i++){
					string+="<tr><td width=\"60px\">"+data[i].cid+"</td>";
					string+="<td width=\"110px\">"+data[i].cname+"</td>";
					string+="<td width=\"60px\">"+data[i].ctel+"</td>";
					string+="<td width=\"110px\">"+data[i].caddress+"</td>";
					string+="<td width=\"60px\">"+data[i].cremark+"</td>";
					string+="<td><input type=\"button\" value=\"删除\"";
					string+="onclick=\"deleteUser("+data[i].cid+")\" /> <input type=\"button\"";
					string+="value=\"修改\" onclick=\"updateContact("+data[i].cid+")\" /></td>";
					string+="</tr>";
				}
				
				$("#tb1").html(string);
				$("#mess").html("");
			},
			error:function(){
				alert("sdjjjj");
			}
			
			
		});
		
	}
	
</script>

<body style="background-color: aquamarine;">
	<div>
		<span
			style="margin-left: 50px; margin-top: 10px; font-size: large; color: blue;">欢迎您:<font
			color="red" size="15px">${user.uname }</font></span>
	</div>
	<div
		style="text-align: center; height: 100px; background-color: green; margin-bottom: 1px;">
		<h1 style="line-height: 100px;">主 界 面</h1>

	</div>
	<div style="width: 19%; height: 400px; background-color: silver; margin-top: 20px; float: left; text-align: center;">
	 <div>
	 	<input type="button" value="添加联系人" onclick="addContact()" >
	 </div>
	  <div>
	 	<a href="${pageContext.request.contextPath }/userServlet?action=tuichu" >退出</a>

	 </div>
	<div id="div1">
	
	</div>
	<div>
	 	<input type="text" value=""   id="ts">
	 	<input type="button" onclick="findCons()" value="搜索">
	 </div>
	
	</div>

	<div 
		style="width: 80%; height: 400px; background-color: #62D1F2; margin-top: 20px; text-align: center; float: right; overflow: auto;">
		<div id="mess"><font size="5" color="blue" style="p: 90px;">${message }</font></div>
			
			<table border="1px" cellspacing="0px" style="text-align: center;">
					<tr>
						<td width="80px">id</td>
						<td width="80px">姓名</td>
						<td width="80px">电话</td>
						<td width="80px">地址</td>
						<td width="80px">备注</td>
						<td>操作</td>
					</tr>
				
			<tbody id="tb1">
			
			
			</tbody>
			</table>
		</div>







	





</body>
<script type="text/javascript">
	function addContact() {
		window.location.href = "${pageContext.request.contextPath}/addContact.jsp";
	}
	function deleteUser(id) {
		if (confirm("确认删除吗?")) {
			
			document.location.href = "${pageContext.request.contextPath}/userServlet?action=userDeleteContact&cid="+ id + "";
		};
		
	}
	function updateContact(id) {

		document.location.href = "${pageContext.request.contextPath}/contactServlet?action=updateContact&cid="+ id + "";
	}
	function findCons(){
		
		var condition =$("#ts").val();
		$.ajax({
			url:"contactServlet",
			type:"post",
			cache:"false",
			data:{"action":"findCons","condition":condition},
			success:function(data){
				var string="";
				for(var i=0;i<data.length;i++){
					string+="<tr><td width=\"60px\">"+data[i].cid+"</td>";
					string+="<td width=\"110px\">"+data[i].cname+"</td>";
					string+="<td width=\"60px\">"+data[i].ctel+"</td>";
					string+="<td width=\"110px\">"+data[i].caddress+"</td>";
					string+="<td width=\"60px\">"+data[i].cremark+"</td>";
					string+="<td><input type=\"button\" value=\"删除\"";
					string+="onclick=\"deleteUser("+data[i].cid+")\" /> <input type=\"button\"";
					string+="value=\"修改\" onclick=\"updateContact("+data[i].cid+")\" /></td>";
					string+="</tr>";
				}
				
				$("#tb1").html(string);
				$("#mess").html("");
			},
			error:function(){
				
			}
			
			
		});
	}
	
</script>
</html>